import $util from '@/utils/index.js'
import $api from '@/api/index.js'
import $store from '@/store/index.js'
import {
	req
} from '@/utils/req.js'
export default {
	state: {
		autograph: '',
		userInfo: '',
		appLogin: '',
		commonOptions: {
			coupon_atv_id: 0,
			channel_id: 0
		},
		location: {},
		wxlocation: {},
		isShowAuth: true,
		mineInfo: {},
		userPageType: 1, // 1用户，2服务
		pageActive: false,
		activeIndex: 0,
		param: {
			page: 1,
			sort: ''
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
	},
	mutations: {
		//更新内容
		async updateUserItem(state, item) {
			let {
				key,
				val
			} = item
			if (key == 'userInfo') {
				let {
					phone = '',
						create_time,
				} = val
				if (phone) {
					val.split_phone = phone.substring(0, 3) + '****' + phone.substring(7, 11)
				}
				val.create_date = $util.formatTime(create_time * 1000, 'YY-M-D')
			}
			state[key] = val
			if (['autograph', 'userInfo', 'location', 'appLogin'].includes(key)) {
				uni.setStorageSync(key, val)
			}
		},
		async updateServiceItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		//获取个人信息
		async getUserInfo({
			commit,
			state
		}, param) {
			let data = await $api.user.userInfo()
			console.error('============user.js getUserInfo', data)
			data.nickName = ""
			data.avatarUrl = ""
			data.nickName = data.user_nicename
			data.avatarUrl = data.avatar
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		//获取用户个人中心数据
		async getMineInfo({
			commit,
			state
		}, param) {
			let data = await $api.mine.index()
			let {
				fx_status
			} = data
			// debugger
			data.nickName = ""
			data.avatarUrl = ""
			data.nickName = data.user_nicename
			data.avatarUrl = data.avatar
			commit('updateUserItem', {
				key: 'mineInfo',
				val: data
			})
			console.log('============user.js getMineInfo', data)
		},
		// 获取用户信息
		async getAuthUserProfile({
			commit,
			state
		}, param) {
			let {
				nickName,
				avatarUrl
			} = param
			console.log('============user.js getAuthUserProfile00', param)
			await $api.user.userUpdate(param)
			let data = Object.assign(state.userInfo, {
				nickName,
				avatarUrl
			})
			console.log('============user.js getAuthUserProfile', data)
			// data.nickName = data.user_nicename
			// data.avatarUrl = data.avatar
			// data.phone = data.mobile
			// data.user_nicename = data.nickName
			// data.avatar = data.avatarUrl
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		// 获取手机号
		async getAuthPhone({
			commit,
			state
		}, {
			e = {
				detail: {}
			},
			must = false
		} = {}) {
			let {
				encryptedData = '', iv = ''
			} = e.detail
			let phone = ''
			if (encryptedData && iv) {
				let phone = await $api.user.reportPhone({
					encryptedData,
					iv
				})
				console.log(phone, '=====phone')
				let data = Object.assign(state.userInfo, {
					phone
				})
				console.log('============user.js getAuthPhone', data)
				commit('updateUserItem', {
					key: 'userInfo',
					val: data
				})
				return phone
			}
		},
		// 更新公共参数
		async updateCommonOptions({
			commit,
			state
		}, param) {
			let target = {}
			if (param.scene) {
				let res = await $api.base.getWxCodeData({
					code_id: param.scene
				})
				target = Object.assign({}, state.commonOptions, res.data)
			} else {
				target = Object.assign({}, state.commonOptions, param)
			}
			let data = $util.pick(target, ['id', 'coupon_atv_id', 'admin_id', 'channel_id'])
			commit('updateUserItem', {
				key: 'commonOptions',
				val: data
			})
			console.log(target, '======target')
			return target
		}
	},
	// 服务
	async getServiceList({
		commit,
		state
	}, param) {
		let d = await $api.service.getServiceList(param)
		let oldList = state.list
		let newList = d
		let list = {}
		if (param.page == 1) {
			list = newList
		} else {
			newList.data = oldList.data.concat(newList.data)
			list = newList
		}
		commit('updateServiceItem', {
			key: 'list',
			val: list
		})
	},
	// 爱我，我爱，老师
	async getUserList({
		commit,
		state
	}, param) {
		let d = await $api.service.getUserList(param)
		let oldList = state.list
		let newList = {
			data: d,
			last_page: 1,
			current_page: 1
		}
		let list = {
			data: [],
			last_page: 1,
			current_page: 1
		}
		if (param.page == 1) {
			list = newList
		} else {
			newList.data = oldList.data.concat(newList.data)
			list = newList
		}
		commit('updateServiceItem', {
			key: 'list',
			val: list
		})
	},
	// 店铺
	async getShopList({
		commit,
		state
	}, param) {
		let d = await $api.service.getShopList(param)
		let oldList = state.list
		let newList = {
			data: d,
			last_page: 1,
			current_page: 1
		}
		let list = {
			data: [],
			last_page: 1,
			current_page: 1
		}
		if (param.page == 1) {
			list = newList
		} else {
			newList.data = oldList.data.concat(newList.data)
			list = newList
		}
		commit('updateServiceItem', {
			key: 'list',
			val: list
		})
	},
	// 免单
	async getFreeList({
		commit,
		state
	}, param) {
		let d = await $api.service.getFreeList(param)
		let oldList = state.list
		let newList = {
			data: d,
			last_page: 1,
			current_page: 1
		}
		let list = {
			data: [],
			last_page: 1,
			current_page: 1
		}
		if (param.page == 1) {
			list = newList
		} else {
			newList.data = oldList.data.concat(newList.data)
			list = newList
		}
		commit('updateServiceItem', {
			key: 'list',
			val: list
		})
	}
}
